<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>订单列表</title>
    <#-- 通用样式-->
    <#include "../common/head.ftl">
</head>
<body>

<div id="wrapper" class="toggled">

    <#--边栏sidebar-->
    <#include "../common/nav.ftl">

    <#--主要内容content-->
    <div id="page-content-wrapper">
        <div class="container-fluid">
            <div class="row clearfix">
                <!-- 表格 -->
                <div class="col-md-12 column">
                    <table class="table table-bordered table-condensed">
                        <thead>
                        <tr>
                            <th>订单编号</th>
                            <th>买家编号</th>
                            <th>买家姓名</th>
                            <th>买家电话</th>
                            <th>买家地址</th>
                            <th>订单总价</th>
                            <th>订单状态</th>
                            <th>支付状态</th>
                            <th colspan="2">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <#list orderDtoList as orderDto>
                        <tr>
                            <td>${orderDto.id}</td>
                            <td>${orderDto.buyerId}</td>
                            <td>${orderDto.buyerName}</td>
                            <td>${orderDto.buyerPhone}</td>
                            <td>${orderDto.buyerAddress}</td>
                            <td>${orderDto.amount}</td>
                            <td>${orderDto.orderStatusEnum.getMessage()}</td>
                            <td>${orderDto.payStatusEnum.getMessage()}</td>
                            <td>
                                <a href="/seller/order/detail?id=${orderDto.id}">详情</a>
                            </td>
                            <td>
                                <#if orderDto.getOrderStatusEnum().message == "新订单">
                                    <a href="/seller/order/cancel?id=${orderDto.id}">取消</a>
                                </#if>
                            </td>
                        </tr>
                        </#list>
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                <div class="col-md-12 column">
                    <ul class="pagination">
                <#if currentPage lte 1>
                    <li class="disabled"><a href="#">上一页</a></li>
                <#else>
                    <li><a href="/seller/order/list?page=${currentPage - 1}&pageSize=${pageSize}">上一页</a></li>
                </#if>

                <#list 1..pageNum as index>
                    <#if currentPage == index>
                            <li class="disabled"><a href="#">${index}</a></li>
                    <#else>
                            <li><a href="/seller/order/list?page=${index}&pageSize=${pageSize}">${index}</a></li>
                    </#if>
                </#list>

                <#if currentPage gte pageNum>
                    <li class="disabled"><a href="#">下一页</a></li>
                <#else>
                    <li><a href="/seller/order/list?page=${currentPage + 1}&pageSize=${pageSize}">下一页</a></li>
                </#if>
                    </ul>
                </div>

            </div>
        </div>
    </div>
</div>

<#--  新订单弹窗提醒  -->
<div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">
                    提醒
                </h4>
            </div>
            <div class="modal-body">
                你有新的订单
            </div>
            <div class="modal-footer">
                <button onclick="javascript:document.getElementById('notice').pause()" type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button onclick="location.reload()" type="button" class="btn btn-primary">查看新的订单</button>
            </div>
        </div>
    </div>
</div>

<#--  播放音乐  -->
<audio id="notice" loop="loop">
    <source src="/mp3/song.mp3" type="audio/mpeg" />
</audio>

<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>

    var websocket = null;
    //判断当前浏览器是否支持WebSocket
    if('WebSocket' in window) {
        websocket = new WebSocket('ws://localhost:8080/webSocket');  //使用WebSocket协议去连接后台
    }else {
        alert('该浏览器不支持websocket!');
    }

    //连接成功建立的回调方法
    websocket.onopen = function (event) {
        console.log('建立连接');
    }

    //关闭连接时的回调方法
    websocket.onclose = function (event) {
        console.log('连接关闭');
    }

    //接收到消息的回调方法
    websocket.onmessage = function (event) {
        console.log('收到消息:' + event.data)
        //弹窗提醒, 播放音乐
        $('#myModal').modal('show');

        document.getElementById('notice').play();
    }

    //连接发生错误的回调方法
    websocket.onerror = function () {
        alert('websocket通信发生错误！');
    }

    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function () {
        websocket.close();
    }

</script>

</body>
</html>